package com.scau.clothes.stock.service.impl;

import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.model.CnameConfiguration;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scau.clothes.core.util.CheckObjectIsNullUtils;
import com.scau.clothes.stock.mapper.ClothesNormMapper;
import com.scau.clothes.stock.model.dto.ClothesNormDto;
import com.scau.clothes.stock.model.entity.ClothesNorm;
import com.scau.clothes.stock.model.entity.Norm;
import com.scau.clothes.stock.service.ClothesNormService;
import com.scau.clothes.stock.service.NormService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClothesNormServiceImpl extends ServiceImpl<ClothesNormMapper, ClothesNorm> implements ClothesNormService {

    @Resource
    private ClothesNormMapper clothesNormMapper;
    @Resource
    private NormService normService;

    private CheckObjectIsNullUtils checkObjectIsNullUtils = new CheckObjectIsNullUtils();

    @Override
    public int insertClothesNorm(ClothesNormDto clothesNormDto) {
        if (!checkObjectIsNullUtils.objCheckIsNull(clothesNormDto)){
            QueryWrapper<Norm> normQueryWrapper = new QueryWrapper<>();
            normQueryWrapper.eq("color_id", clothesNormDto.getColorId());
            normQueryWrapper.eq("size_id", clothesNormDto.getSizeId());
            normQueryWrapper.ne("is_delete", 1);
            Norm norm = normService.getOne(normQueryWrapper);
            if (norm == null){
                Norm newNorm = new Norm();
                newNorm.setColorId(clothesNormDto.getColorId());
                newNorm.setSizeId(clothesNormDto.getSizeId());
                normService.insertNorm(newNorm);
                QueryWrapper<Norm> newNormQueryWrapper = new QueryWrapper<>();
                newNormQueryWrapper.eq("color_id", clothesNormDto.getColorId());
                newNormQueryWrapper.eq("size_id", clothesNormDto.getSizeId());
                norm = normService.getOne(newNormQueryWrapper);
            }
            ClothesNorm clothesNorm = new ClothesNorm();
            BeanUtils.copyProperties(clothesNormDto, clothesNorm);
            if (StrUtil.isBlank(clothesNorm.getNormSn()))
                clothesNorm.setNormSn(norm.getNormSn());
            clothesNorm.setNorm(norm);
            return clothesNormMapper.insert(clothesNorm);
        }
        return 0;
    }

    @Override
    public int updateClothesNorm(ClothesNorm clothesNorm) {
        return clothesNormMapper.updateById(clothesNorm);
    }

    @Override
    public int deleteClothesNorm(ClothesNorm clothesNorm) {
        clothesNorm.setIsDelete(1);
        return clothesNormMapper.updateById(clothesNorm);
    }

    @Override
    public List<ClothesNorm> listClothesNorm(ClothesNormDto clothesNormDto) {
        return clothesNormMapper.listAllClothesNorm(clothesNormDto);
    }
}
